package org.oss.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;

import javax.net.ssl.SSLContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.config.SocketConfig;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContextBuilder;

import sun.net.www.http.HttpClient;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.common.comm.IdleConnectionReaper;
import com.aliyun.oss.common.comm.Protocol;

public class HttpClientUtil {
	/**
	 * 日志处理类
	 */
	private static final Log log = LogFactory.getLog(HttpClientUtil.class);

	// 读取超时
	private final static int SOCKET_TIMEOUT = 10000;
	// 连接超时
	private final static int CONNECTION_TIMEOUT = 10000;
	// 每个HOST的最大连接数量
	private final static int MAX_CONN_PRE_HOST = 20;
	// 连接池的最大连接数
	private final static int MAX_CONN = 60;

	

	public static HttpClientConnectionManager getDefaultConnectionManager(){
			SSLContext sslContext = null;
	        try {
	            sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { 
	                
	                public boolean isTrusted(X509Certificate[] chain, String authType)
	                        throws CertificateException {
	                    return true;
	                }
	                
	            }).build();
	            
	        } catch (Exception e) {
	            throw new ClientException(e.getMessage());
	        }
	        
	        SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
	        Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
	                .register(Protocol.HTTP.toString(), PlainConnectionSocketFactory.getSocketFactory())
	                .register(Protocol.HTTPS.toString(), sslSocketFactory)
	                .build();
			PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
	        connectionManager.setDefaultMaxPerRoute(MAX_CONN);
	        connectionManager.setMaxTotal(MAX_CONN);
	        connectionManager.setDefaultSocketConfig(SocketConfig.custom().
	                setSoTimeout(SOCKET_TIMEOUT).setTcpNoDelay(true).build());
	        connectionManager.setValidateAfterInactivity(-1);
	        return connectionManager;
	}
	
	
	public static CloseableHttpClient getHttpClient(HttpClientConnectionManager httpConnectionManager){
		if(null != httpConnectionManager){
			IdleConnectionReaper.registerConnectionManager(httpConnectionManager);
		}
		
		CloseableHttpClient httpClient =HttpClients.custom().setConnectionManager(httpConnectionManager)
		        .disableContentCompression()
		        .disableAutomaticRetries()
		        .build();
		return httpClient;
	}
	
	

	

	
	
	
}
